import type { EChartsOption } from 'echarts'
import { random } from 'lodash-es'

export function mockTableData(columns: string[], maxTotal = 10) {
  return Array(random(0, maxTotal))
    .fill('')
    .map((_) => {
      return Object.fromEntries(
        columns.map((col) => [
          col,
          Array(random(1, 10))
            .fill('')
            .map(() => String.fromCharCode(random(19968, 40869)))
            .join(''),
        ]),
      )
    })
}

export function mockTableColumns(props: string[], labels: string[]) {
  return props.map((p, i) => ({
    property: p,
    label: labels[i],
    minWidth: labels[i].length > 4 ? 120 : 100,
  }))
}

export function mockTable(props: string[], labels: string[], maxTotal = 10) {
  const columns = ref<any[]>([])
  const data = ref<any[]>([])

  function load() {
    columns.value = mockTableColumns(props, labels)
    data.value = mockTableData(props, maxTotal)
  }
  load()

  function loadRows(arr: any[][]) {
    data.value = arr.map((items) =>
      props.reduce((obj, p, i) => {
        obj[p] = items[i]
        return obj
      }, {} as any))
  }

  return {
    columns,
    data,
    load,
    loadRows,
  }
}

export function mockAxisChart(
  type: 'bar' | 'line',
  title: string,
  xAxisData: string[],
  maxYAxisData = 1000,
) {
  return {
    title: {
      text: title,
    },
    xAxis: {
      type: 'category',
      data: xAxisData,
    },
    yAxis: {
      type: 'value',
    },
    series: {
      type,
      data: Array(xAxisData.length)
        .fill(0)
        .map(() => random(0, maxYAxisData)),
    },
  } as EChartsOption
}

export function mockPieChart(title: string, categories: string[], maxNum = 1000) {
  return {
    title: {
      text: title,
    },
    series: {
      type: 'pie',
      label: {
        show: true,
      },
      radius: ['50%', '70%'],
      data: categories.map((c) => ({ value: random(1, maxNum), name: c })),
    },
  } as EChartsOption
}
